package com.deyuanyun.pic.settings.task;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.log4j.Logger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.deyuanyun.pic.common.exception.MyUncaughtExceptionHandler;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.settings.controller.dto.SMSMessageVO;

/**
 * 队列列表
 * @author chace
 * Created by xu on 2016/4/1.
 */
public class Pool {
    static Logger logger = Logger.getLogger(Pool.class);
    /**短信队列*/
    public final static BlockingQueue<SMSMessageVO> SMS_QUEUE=new ArrayBlockingQueue<SMSMessageVO>(100);
    /**用来控制短信线程存在的数量*/
    public static boolean sms_mult_thread=false;

    /**线程池*/
    public static ThreadPoolTaskExecutor threadPoolTaskExecutor;
    static {
        threadPoolTaskExecutor = (ThreadPoolTaskExecutor) SpringAppliction.getBean("taskExecutor",ThreadPoolTaskExecutor.class);
        ThreadPoolExecutor t=threadPoolTaskExecutor.getThreadPoolExecutor();
        t.setThreadFactory(new HandlerThreadFactory());
    }

}

/**用来替代ThreadFactory*/
class HandlerThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        //System.out.println("created " + t + " ID:" + t.getId());
        t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
        //System.out.println("eh=" + t.getUncaughtExceptionHandler());
        return t;
    }
}
